package dao.impl;

import dao.BaseDao;
import dao.PrescriptionDao;
import entity.CheckItem;
import entity.Department;
import entity.Prescription;
import utils.ConnectionUtil;
import vo.PrescriptionCheckItemDepartmentVo;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @projectName: Hospital
 * @package: dao.impl
 * @className: PrescriptionDaoImpl
 * @author: 翟
 * @date: 2025/4/1 9:41
 */

public class PrescriptionDaoImpl extends BaseDao implements PrescriptionDao {
    public PrescriptionDaoImpl(Connection connection) {
        super(connection);
    }

    @Override
    public List<PrescriptionCheckItemDepartmentVo> getAllPrescriptionsByPatientId(int patientId) throws SQLException {
        /**
         *         select d.depName , c.checkItemName , pre.checkResult , c.checkItemCost , pre.examDate from prescription pre
         *         inner join patient p on pre.patientId = p.patientId
         *         inner join checkItem c on pre.checkItemId = c.checkItemId
         *         inner join depAtmEnt d on pre.depId = d.depId
         */
        String sql = "SELECT d.depName , c.checkItemName , pre.checkResult , c.checkItemCost , pre.examDate FROM prescription pre\n" +
                "INNER JOIN patient p ON pre.patientId = p.patientId\n" +
                "INNER JOIN checkItem c ON pre.checkItemId = c.checkItemId\n" +
                "INNER JOIN depAtmEnt d ON pre.depId = d.depId\n" +
                "WHERE pre.patientId = ?";
        ResultSet resultSet = null;
        try {
            resultSet = executeQuery(sql , patientId);
            List<PrescriptionCheckItemDepartmentVo> list = new ArrayList<>();
            while (resultSet.next()){
                PrescriptionCheckItemDepartmentVo prescriptionCheckItemDepartmentVo = new PrescriptionCheckItemDepartmentVo();
                prescriptionCheckItemDepartmentVo.setCheckResult(resultSet.getString("checkResult"));
                prescriptionCheckItemDepartmentVo.setExamDate(resultSet.getDate("examDate"));
                prescriptionCheckItemDepartmentVo.setCheckItemName(resultSet.getString("checkItemName"));
                prescriptionCheckItemDepartmentVo.setCheckItemCost(resultSet.getDouble("checkItemCost"));
                prescriptionCheckItemDepartmentVo.setDepName(resultSet.getString("depName"));
                list.add(prescriptionCheckItemDepartmentVo);
            }
            return list;
        } finally {
            ConnectionUtil.closeALL(resultSet , null , null);
        }
    }
}
